/**
  ******************************************************************************
  * @file    timer_software.h
  * @author  ashuai0110
  * @version V2.3
  * @date    2024-07-01
  * @brief   软件定时器
  *
  ******************************************************************************
  * @attention
  *
  * 版权声明:内容为编者(ashuai0110)原创,使用请注明出处,当然,你也可以不这样做^_^
  * 出处链接:https://gitee.com/ashuai0110/mcu_reuse_development_module.git
  *
  ******************************************************************************
  */

#ifndef __MULTITIMERS_H
#define __MULTITIMERS_H

#ifdef __cplusplus
extern "C" {
#endif

/* 包含头文件-----------------------------------------------------------------*/
#include "common_include.h"

/**
 * @addtogroup modules
 * @{
 */

/**
 * @addtogroup timer_software
 * @{
 */

/* 宏定义---------------------------------------------------------------------*/
/**
 * @defgroup timer_software_global_macros timer software global macros
 * @{
 */

/**
 * @defgroup timer_run_flg timer run flag
 * @{
 */
#define TIMER_RUN_STOP                      (0u) /*!< 不运行 */
#define TIMER_RUN_START                     (1u) /*!< 运行 */
/**
 * @}
 */

/**
 * @}
 */

/* 类型定义-------------------------------------------------------------------*/
/**
 * @defgroup timer_software_global_types timer software global types
 * @{
 */

/** 
  * @brief  软件定时器任务 structrue definition
  */
typedef struct timer_s {
    volatile uint32_t curTicks; /*!< 当前计数值ms */
    uint32_t targetTicks;       /*!< 目标计数值ms */
    uint8_t runFlg : 1;         /*!< 运行标志 @ref timer_run_flg */
    void(* callbackFn)(void);   /*!< 回调函数 */
    struct timer_s *pNext;      /*!< 下一节点 */
} timer_s_t;

/**
 * @}
 */

/* 全局变量-------------------------------------------------------------------*/

/* 函数原型-------------------------------------------------------------------*/
/**
 * @addtogroup timer_software_global_functions
 * @{
 */

uint8_t timer_s_init(timer_s_t *_ts, uint32_t targetTicks, void (* callbackFn)(void), uint8_t runFlg);
void timer_s_de_init(timer_s_t *_ts);

void timer_s_poll(void);

/* list interface */
uint8_t timer_s_insert(timer_s_t *_ts);
uint8_t timer_s_pending(timer_s_t *_ts);

/* set/get interface */
void timer_s_set_cur_ticks(timer_s_t *_ts, uint32_t curTicks);
uint32_t timer_s_get_cur_ticks(timer_s_t *_ts);
void timer_s_set_target_ticks(timer_s_t *_ts, uint32_t targetTicks);
uint32_t timer_s_get_target_ticks(timer_s_t *_ts);
void timer_s_set_run(timer_s_t *_ts, uint8_t runFlg);
uint8_t timer_s_get_run(timer_s_t *_ts);

/**
 * @}
 */

/**
 * @}
 */

/**
 * @}
 */

#ifdef __cplusplus
}
#endif

#endif /* timer_software.h */
